package com.pay.Essearch.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pay.Essearch.mapper.EsearchMapper;
import com.pay.Essearch.pojo.TGoods;
import com.pay.Essearch.service.EsearchService;
import com.pay.Essearch.vo.EsSearchVo;
import com.pay.common.utils.Result;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @author 28242
* @description 针对表【t_goods(商品表)】的数据库操作Service实现
* @createDate 2025-08-08 15:00:59
*/
@Service
@Slf4j
public class EsearchServiceImpl extends ServiceImpl<EsearchMapper, TGoods>
    implements EsearchService {


    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public Result queryEsGoods(EsSearchVo esSearchVo) {
        Integer pageNum = 0;
        if (esSearchVo.getPageNum() >0){
            pageNum = esSearchVo.getPageNum()-1;
        }
        PageRequest pageRequest = PageRequest.of(pageNum, esSearchVo.getPageSize());

        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
        if (esSearchVo.getGoodName() != null && esSearchVo.getGoodName().length() >0){
            queryBuilder.must(QueryBuilders.matchQuery("goodName",esSearchVo.getGoodName()));
        }
        if (esSearchVo.getStartprice() != null  && esSearchVo.getStartprice() >0){
            queryBuilder.must(QueryBuilders.rangeQuery("price").gte(esSearchVo.getStartprice()));
        }
        if (esSearchVo.getEndprice() != null && esSearchVo.getEndprice() >0){
            queryBuilder.must(QueryBuilders.rangeQuery("price").lte(esSearchVo.getEndprice()));
        }


        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withPageable(pageRequest)
                .withQuery(queryBuilder);
        SearchHits<TGoods> searchHits = elasticsearchRestTemplate.search(builder.build(), TGoods.class);

        long total = searchHits.getTotalHits();
        ArrayList<TGoods> tGoods = new ArrayList<>();
        for (SearchHit<TGoods> searchHit : searchHits.getSearchHits()) {
            TGoods content = searchHit.getContent();
            tGoods.add(content);
        }

        HashMap<String, Object> map = new HashMap<String, Object>() {{
            put("total", total);
            put("list", tGoods);
        }};

        return Result.ok().setData(map);

    }


}




